*==============================================================================*
*    ______                                       __  _           
*   / ____/________  __________   ________  _____/ /_(_)___  ____ 
*  / /   / ___/ __ \/ ___/ ___/  / ___/ _ \/ ___/ __/ / __ \/ __ \
* / /___/ /  / /_/ (__  |__  )  (__  )  __/ /__/ /_/ / /_/ / / / /
* \____/_/   \____/____/____/  /____/\___/\___/\__/_/\____/_/ /_/ 
*                                                                
*==============================================================================*
* Import data
use "DatasetDemo.dta", clear 

* Global definitions
global all_var GSearch SameState Cit10k Distance ///
				News LocalNewsPaper PercAdvImp /// 
				Vol NegRet //
global y GSearch
global x SameState
global x_cit Cit10k
global x_dist Distance
global x_inter Vol SameStateXVol LocalNewsPaper SameStateXLocalNewsPaper News SameStateXNews SameStateXPercAdvImp
global x_inter_realized VolRealized SameStateXVolRealized LocalNewsPaper SameStateXLocalNewsPaper News SameStateXNews SameStateXPercAdvImp
global x_exog LocalNewsPaper SameStateXLocalNewsPaper News SameStateXNews SameStateXPercAdvImp
global x_endog c.Vol c.SameState#c.Vol
global x_intrument InstrumVol3 c.SameState#c.InstrumVol3
global x_no_inter Vol LocalNewsPaper News
global x_RiskNews1 StdNews SameStateXStdNews AggrNews SameStateXAggrNews StdLocalNewsPaper SameStateXStdLocalNewsPaper Vol SameStateXVol VolMRealized SameStateXVolM SameStateXPercAdvImp
global x_RiskNews2 StdNews SameStateXStdNews AggrNews SameStateXAggrNews StdLocalNewsPaper SameStateXStdLocalNewsPaper Vol SameStateXVol Vix SameStateXVix SameStateXPercAdvImp
global x_RiskNews3 StdNews SameStateXStdNews AggrNews SameStateXAggrNews StdLocalNewsPaper SameStateXStdLocalNewsPaper SistRisk SameStateXSistRisk SpecRisk SameStateXSpecRisk SameStateXPercAdvImp

global c_time i.Week
global c_unit i.Unit
global c_state i.State
global spec absorb(Unit State Week) vce(cluster Unit) 
global specIV absorb(Unit State Week) robust
				
* Table 1		
fsum $all_var, s(N mean SD min max)

* Table 3
eststo clear
qui: eststo: reghdfe $y $x, $spec
qui: eststo: reghdfe $y $x_cit, $spec
qui: eststo: reghdfe $y $x_dist, $spec
qui: eststo: reghdfe $y $x $x_inter, $spec
* Computationally intensive
* qui: eststo: ivreghdfe $y $x $x_exog ($x_endog = $x_intrument), $specIV 
*qui: reg $y $x $x_inter $c_state $c_time $c_unit if NegRet==0
*estimate store POS	
*qui: reg $y $x $x_inter $c_state $c_time $c_unit if NegRet==1 
*estimate store NEG
*eststo: suest POS NEG, vce(cluster Unit)
*test  [POS_mean]Vol - [NEG_mean]Vol = 0
*test [POS_mean]SameStateXVol - [NEG_mean]SameStateXVol = 0
*test  ([POS_mean]Vol - [NEG_mean]Vol = 0) ([POS_mean]SameStateXVol - [NEG_mean]SameStateXVol = 0)
qui: eststo: reghdfe $y $x $x_inter_realized, $spec
esttab, b(2) t(2) star(* 0.10 ** 0.05 *** 0.01) brackets noomitted ar2 replace noomitted varwidth(20) 

* Table 4
eststo clear
qui: eststo: reghdfe $y $x $x_RiskNews1, $spec
qui: eststo: reghdfe $y $x $x_RiskNews2, $spec
qui: eststo: reghdfe $y $x $x_RiskNews3, $spec
esttab, b(2) t(2) star(* 0.10 ** 0.05 *** 0.01) brackets noomitted ar2 replace nogaps varwidth(30)

* Table A1
global x_holi c.SameState##i.Holi
capture drop ContGSearch
gen ContGSearch = log((GSearch+0.01)/(100-GSearch+0.01))
global y_cont ContGSearch

eststo clear
qui: eststo: reghdfe $y $x if SampleSmallState==1, $spec
qui: eststo: reghdfe $y $x if TickerLen>1, $spec
qui: eststo: reghdfe $y $x $x_holi, $spec
qui: eststo: reghdfe $y_cont $x, $spec
qui: eststo: reghdfe $y $x $x_inter if SampleSmallState==1, $spec
qui: eststo: reghdfe $y $x $x_inter if TickerLen>1, $spec
qui: eststo: reghdfe $y $x $x_inter $x_holi, $spec
qui: eststo: reghdfe $y_cont $x $x_inter, $spec
esttab, b(2) t(2) star(* 0.10 ** 0.05 *** 0.01) brackets noomitted ar2 replace nogaps varwidth(30)

*==============================================================================*
*   _______                                  _          
*  /_  __(_)___ ___  ___     ________  _____(_)__  _____
*   / / / / __ `__ \/ _ \   / ___/ _ \/ ___/ / _ \/ ___/
*  / / / / / / / / /  __/  (__  )  __/ /  / /  __(__  ) 
* /_/ /_/_/ /_/ /_/\___/  /____/\___/_/  /_/\___/____/  
*                                                       
*==============================================================================*
use "DatasetDemo.dta", clear 

* Variable generation
egen sum_GSearch = sum(GSearch), by (Unit Week)
gen PropGSearchState = GSearch/sum_GSearch
gen PropGSearchOther = 1 - PropGSearchState
drop sum_GSearch
sum PropGSearchState if Unit == 1 & Week == 2
di r(sum)

drop if SameState == 0 
egen sum_GSearchUS = sum(GSearchUS), by(Unit)
gen PropGSearchUS = GSearchUS/sum_GSearchUS
drop sum_GSearchUS
sum PropGSearchUS if Unit == 1
di r(sum)

gen LocalSearch = PropGSearchState*PropGSearchUS*100
gen NonLocalSearch = PropGSearchOther*PropGSearchUS*100 

global to_trim aveSpread
foreach x in $to_trim {
		gen trim_`x' = `x'
		centile `x', centile(1 99)
		replace trim_`x' = . if (trim_`x' > r(c_2) | trim_`x' < r(c_1))
		}

gen LnLocalSearch = ln(LocalSearch)
gen LnNonLocalSearch = ln(NonLocalSearch)
replace	trim_aveSpread = trim_aveSpread*100	
ren trim_aveSpread Spread
replace Vol = Vol*100
replace VolRealized = VolRealized*100


* Global definitions
global y1 LocalSearch
global y2 NonLocalSearch
global y3 Vol
global y4 Spread
global y5 VolRealized 
global y6 Ret

* Unit root test
*xtunitroot fisher $y1, pperron lags(0)
*xtunitroot fisher $y2, pperron lags(0)
*xtunitroot fisher $y3, pperron lags(0)
*xtunitroot fisher $y4, pperron lags(0)
*xtunitroot fisher $y5, pperron lags(0)
*xtunitroot fisher $y6, pperron lags(0)

* Table 7
eststo clear
qui: eststo: pvar $y1 $y2, vce(robust)
qui: eststo: pvar $y1 $y2 $y3, vce(robust)
qui: eststo: pvar $y1 $y2 $y4, vce(robust)
qui: eststo: pvar $y1 $y2 $y5, vce(robust)
esttab , b(2) t(2) star(* 0.10 ** 0.05 *** 0.01) brackets noomitted ar2 replace nogaps varwidth(30) 

